package musictube.client;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestionEvent;
import com.google.gwt.user.client.ui.SuggestionHandler;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Song selection panel sized for a web page.
 * 
 * @author Russell Souza
 */
public class WebSongSelector extends VerticalPanel {

	/**
	 * The database interface.
	 */
	private final IDatabase m_database;

	/**
	 * The song player.
	 */
	private final HiddenIFrameSongPlayer m_songPlayer;

	/**
	 * The song list box.
	 */
	private ListBox m_listBox;

	/**
	 * Constructor.
	 */
	public WebSongSelector(IDatabase p_database,
			HiddenIFrameSongPlayer p_songPlayer) {

		// assign the member variables
		m_database = p_database;
		m_songPlayer = p_songPlayer;

		// build the panel
		build();

		// set the style name
		setStylePrimaryName("box-Panel");
	}

	/**
	 * Private build method.
	 */
	private void build() {

		// create the all song list box
		m_listBox = new ListBox(false);
		m_listBox.addStyleDependentName("WebSongSelector");
		m_listBox.setVisibleItemCount(10);
		add(m_listBox);

		// call the database to get the list of songs and location
		HashMap map = m_database.getAllItems();

		// iterate for the song list
		MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
		Set keySet = map.keySet();
		Iterator iter = keySet.iterator();
		while (iter.hasNext()) {

			// get the song title and location
			final String title = iter.next().toString();
			final String location = map.get(title).toString();

			// add the item to the list box
			m_listBox.addItem(title, location);

			// add the item to the oracle
			oracle.add(title);
		}

		// add a change listener to the list box
		m_listBox.addChangeListener(new ChangeListener() {
			public void onChange(Widget p_sender) {
				sendSongToPlayer();
			}
		});

		// create the search text box
		final SuggestBox suggestionBox = new SuggestBox(oracle);
		add(suggestionBox);
		suggestionBox.addEventHandler(new SuggestionHandler() {
			public void onSuggestionSelected(SuggestionEvent p_event) {
				int numItems = m_listBox.getItemCount();
				String selectedString = suggestionBox.getText();
				for (int i = 0; i < numItems; i++) {
					if (m_listBox.getItemText(i).equals(selectedString)) {
						m_listBox.setSelectedIndex(i);
						sendSongToPlayer();
					}
				}
			}
		});
	}

	/**
	 * Send the selected song to the player.
	 */
	private void sendSongToPlayer() {

		// get the selected index
		int selectedIndex = m_listBox.getSelectedIndex();

		// get the selected song location
		String songLocation = m_listBox.getValue(selectedIndex);

		// set the song in the player
		m_songPlayer.setSong(songLocation);
	}
}
